package com.liao.mapth;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.amap.api.maps.*;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;


public class MainActivity extends AppCompatActivity implements View.OnClickListener,AMapLocationListener{

    private MapView mapView;    ///初始化地图
    private AMap aMap;

    ///点击定位功能
    private Button locationBt;

    private Marker locationMarker;

    public AMapLocationClient mLocationClient = null;

    public AMapLocationClientOption mLocationOption = null;
    @Override
 /*   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }*/
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//设置对应的XML布局文件

        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写

        if (aMap == null) {
            aMap = mapView.getMap();

            aMap.setMapType(AMap.MAP_TYPE_NORMAL);
            aMap.showBuildings(true);
        }





        initView();

        initLocation();

     /*   LatLng marker1 = new LatLng(30.52033,114.3981);
        //设置中心点和缩放比例
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1));
        aMap.moveCamera(CameraUpdateFactory.zoomTo(16));*/

      /*  MyLocationStyle myLocationStyle;                  //创建定位
        myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);
        myLocationStyle.interval(2000);                  //定位时间请求
        aMap.setMyLocationStyle(myLocationStyle);        //显示小蓝点
        aMap.setMyLocationEnabled(true);*/

    }

    private void initView(){
        locationBt = (Button) findViewById(R.id.location_bt);

        locationBt.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (mLocationClient != null) {
            mLocationClient.startLocation();
        }

    }
    /**
     * 初始化定位相关东西
     */
    private void initLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);


        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);


    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if(aMapLocation != null){
            if(aMapLocation.getErrorCode() == 0){
                LatLng latLng = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLocationType());

                Log.i("location",latLng.toString());

                ///显示定位位置
                if(locationMarker == null){
                    locationMarker = aMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker)));
                } else {
                    locationMarker.setPosition(latLng);
                }

                aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
            } else {
                Log.e("AmapError","location Error:"+aMapLocation.getErrorCode()+"errInfo:"+aMapLocation.getErrorInfo());
            }
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mapView.onDestroy();

        //退出界面的时候停止定位
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
        }

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
